package com.kaiwukj.android.ufamily.mvp.ui.page.mine.address;

import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chauthai.swipereveallayout.ViewBinderHelper;
import com.kaiwukj.android.mcas.utils.UToast;
import com.kaiwukj.android.ufamily.R;
import com.kaiwukj.android.ufamily.mvp.http.entity.result.MyAddressResult;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;

public class MyAddressAdapter extends BaseQuickAdapter<MyAddressResult, BaseViewHolder> {

    private final ViewBinderHelper binderHelper = new ViewBinderHelper();

    @Override
    public long getItemId(int position) {
        return getItem(position).getId();
    }

    public MyAddressAdapter() {
        super(R.layout.item_address_my, new ArrayList<>());
        binderHelper.setOpenOnlyOne(true);
    }

    private OnItemChildClickListener mListener;

    public void setOnItemChildClickListener(OnItemChildClickListener listener) {
        mListener = listener;
    }

    @Override
    protected void convert(@NotNull BaseViewHolder holder, MyAddressResult result) {
        //addChildClickViewIds(R.id.tv_delete, R.id.iv_edit);

        holder.setText(R.id.tv_name, result.getConsigneeName());
        holder.setText(R.id.tv_phone, result.getPhone());
        holder.setText(R.id.tv_address, result.getDetailAddress());
        View defaultFlag = holder.getView(R.id.tv_default);

        binderHelper.bind(holder.getView(R.id.swipe_reveal), String.valueOf(result.getId()));

        if (result.isDefault()) {
            defaultFlag.setVisibility(View.VISIBLE);
        } else {
            defaultFlag.setVisibility(View.GONE);
        }

        View editView = holder.getView(R.id.container_edit);
        View deleteView = holder.getView(R.id.tv_delete);

        holder.getView(R.id.container_item).setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onItemClick(holder.itemView, holder.getLayoutPosition());
            }
        });

        editView.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onItemChildClick(editView, holder.getLayoutPosition());
            }
        });

        deleteView.setOnClickListener(v -> {
            if (mListener != null) {
                mListener.onItemChildClick(deleteView, holder.getLayoutPosition());
            }
        });


    }

    public interface OnItemChildClickListener {
        void onItemChildClick(View view, int pos);

        void onItemClick(View view, int pos);
    }
}
